import path from "path";
import {createTsClient, createTsServer} from "./JsonToTs";
import {config} from "./main";

const fs = require('fs')
const excel = require('exceljs');

/**
 * Excel转Json数据
 * @param {*} src           读取的excel文件目录
 * @param {*} dst           导出的json文件目录
 * @param {*} name          excel文件名
 * @param {*} isClient      是否为客户端数据
 */
async function convert(src: string, dst: string, name: string, isClient: boolean) {

    const workbook = new excel.Workbook();

    // 读取excel
    await workbook.xlsx.readFile(src);
    const worksheets = workbook.worksheets;
    console.log(`读取路径${src} 开始转换${name}文件`);
    worksheets.forEach((worksheet: any) => {
        let r: any = {};
        let names: any[] = [];          // 文名字段名
        let keys: any[] = [];           // 字段名
        let types: any[] = [];          // 通用字段数据类型
        let types_client: any = {};     // 客户端数据类型
        let clients: any[] = [];        // 是否输出客户端字段数据
        let primary: string[] = [];     // 多主键配置
        let primary_index: number[] = [];
        var sheetName = worksheet.name.charAt(0).toUpperCase() + worksheet.name.slice(1);
        var jsonFileName = dst + sheetName + ".json"


        worksheet.eachRow((row: any, rowNumber: number) => {
            let data: any = {};
            row.eachCell((cell: any, colNumber: number) => {
                const value = cell.text;
                // console.warn(cell.text, cell.string, cell.number, cell.result, cell.formula)
                if (rowNumber === 5) {                              // 字段中文名
                    names.push(value);
                    if (value.indexOf("【KEY】") > -1) {
                        primary_index.push(colNumber);
                        primary.push(keys[colNumber - 1]);
                    }
                } else if (rowNumber === 2) {                         // 字段英文名
                    keys.push(value.charAt(0).toUpperCase() + value.slice(1));
                    // if (primary_index.indexOf(colNumber) > -1) primary.push(value);
                } else if (rowNumber === 3) {                         // 通用字段数据类型
                    types.push(value);
                } else if (rowNumber === 4) {    //
                    let indexof = value.indexOf("C");
                    if (indexof < 0) {
                        clients.push(false);
                    } else {
                        clients.push(true);
                    }
                } else if (rowNumber > 5) {
                    let index = colNumber - 1;
                    let type = convertToTsType(types[index]);
                    let client = clients[index];

                    // 验证是否输出这个字段
                    if (client) {
                        let key = keys[index];
                        switch (type) {
                            case "int":
                                // console.warn(`${index}int`, key, value, cell.string, cell.number, cell.result)
                                if (cell.formula) {
                                    data[key] = parseInt(cell.result);
                                } else {
                                    data[key] = parseInt(value);
                                }
                                types_client[key] = {
                                    en: "number",
                                    zh: names[index]
                                };
                                break;
                            case "float":
                                // console.warn(`${index}int`, key, value, cell.string, cell.number, cell.result)
                                if (cell.formula) {
                                    data[key] = parseFloat(cell.result);
                                } else {
                                    data[key] = parseFloat(value);
                                }
                                types_client[key] = {
                                    en: "number",
                                    zh: names[index]
                                };
                                break;
                            case "string":
                                // console.warn(`${index}int`, key, value, cell.string, cell.number, cell.result)
                                data[key] = value;
                                types_client[key] = {
                                    en: "string",
                                    zh: names[index]
                                };
                                break;
                            case "string[]":
                                // console.warn(`${index}int`, key, value, cell.string, cell.number, cell.result)
                                data[key] = value.split(",");
                                types_client[key] = {
                                    en: "string[]",
                                    zh: names[index]
                                };
                                break;
                            case "int[]":
                                // console.warn(`${index}int`, key, value, cell.string, cell.number, cell.result)
                                data[key] = value.split(",");
                                types_client[key] = {
                                    en: "number[]",
                                    zh: names[index]
                                };
                                break;
                            case "any":
                                // console.warn(`${index}int`, key, value, cell.string, cell.number, cell.result)
                                try {
                                    data[key] = JSON.parse(value);
                                    types_client[key] = {
                                        en: "any",
                                        zh: names[index]
                                    };
                                } catch {
                                    console.log('Cell ' + cell.address + ' has value ' + cell.text);
                                    console.warn(`文件【${src}】的【${key}】字段【${data[key]}】类型数据【${value}】JSON转字段串错误【${client}】`);
                                }
                                break;
                        }
                    }
                }
            });

            // 生成数据（多主键）
            if (rowNumber > 5) {
                let temp: any = null;
                for (var i = 0; i < primary.length; i++) {
                    let k = primary[i];
                    let id = data[k];
                    delete data[k];           // 主键数据删除

                    if (primary.length == 1) {
                        r[id] = data;
                    } else {
                        if (i == primary.length - 1) {
                            temp[id] = data;
                        } else if (i == 0) {
                            if (r[id] == undefined) {
                                r[id] = {};
                            }
                            temp = r[id];
                        } else {
                            temp[id] = {};
                            temp = temp[id];
                        }
                    }
                }
            }
        });

        fs.writeFileSync(jsonFileName, JSON.stringify(r));
        // 生成客户端脚本
        createTsClient(sheetName, types_client, r, primary);
        console.log("客户端数据", "生成成功", jsonFileName);
    });

    // const worksheet = workbook.getWorksheet(1);                 // 获取第一个worksheet

}

function convertToTsType(type: string): string {
    switch (type) {
        case "int":
        case "int32":
        case "int64":
            return "int";
        case "float":
        case "float32":
        case "float64":
            return "float";
        case "string":
            return "string";
        case "[]string":
            return "string[]";
        case "[]int":
        case "[]int32":
        case "[]int64":
            return "int[]";
        default:
            return "string"; // Default to string if type is unknown
    }
}

export function run() {
    var inputExcelPath = path.join(__dirname, config.PathExcel.replace("project://", "../../../") + "/");
    var outJsonPathClient = path.join(__dirname, config.PathJsonClient.replace("project://", "../../../") + "/");

    const files = fs.readdirSync(inputExcelPath);
    files.forEach((f: any) => {
        let name = f.substring(0, f.indexOf("."));
        let ext = f.toString().substring(f.lastIndexOf(".") + 1);
        if (ext == "xlsx") {
            convert(inputExcelPath + f, outJsonPathClient, name, true);                   // 客户端数据
        }
    });
}